package stark.app.function.imgedit;

import android.content.Intent;
import android.net.Uri;

import com.bumptech.glide.Glide;
import com.stark.imgedit.EditType;
import com.stark.imgedit.ImgEditEntry;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityImgEditDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;

/**
 * Author：daizhongbin on 2021/4/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class ImgEditDemoActivity extends BaseNoModelActivity<ActivityImgEditDemoBinding> {

    private static final int REQ_EDIT_IMG = 1;

    @Override
    protected int onCreate() {
        return R.layout.activity_img_edit_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnImgMain.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0));
            });
        });
        mDataBinding.btnImgBeauty.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.BEAUTY);
            });
        });
        mDataBinding.btnImgText.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.ADD_TEXT);
            });
        });
        mDataBinding.btnImgRotate.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.ROTATE);
            });
        });
        mDataBinding.btnImgCrop.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.CROP);
            });
        });
        mDataBinding.btnImgFilter.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.FILTER);
            });
        });
        mDataBinding.btnImgSticker.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.STICKER);
            });
        });
        mDataBinding.btnImgPaint.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.PAINT);
            });
        });
        mDataBinding.btnImgColorTune.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.COLOR_TUNE);
            });
        });
        mDataBinding.btnImgMosaic.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEdit(this, paths.get(0), EditType.MOSAIC);
            });
        });
        mDataBinding.btnImgForRet.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEditForRet(this, paths.get(0), REQ_EDIT_IMG);
            });
        });
        mDataBinding.btnImgCallback.setOnClickListener(v -> {
            start(paths -> {
                ImgEditEntry.enterImgEditWithCallback(this, paths.get(0), bitmap -> {
                    mDataBinding.ivRet.setImageBitmap(bitmap);
                });
            });
        });
        mDataBinding.btnPhotoFrameTest.setOnClickListener(v -> {
            start(paths -> PhotoFrameDemoActivity.start(ImgEditDemoActivity.this, paths.get(0)));
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_EDIT_IMG) {
                Uri uri = data.getParcelableExtra(Extra.RET_URI);
                if (uri != null) {
                    Glide.with(this).load(uri).into(mDataBinding.ivRet);
                }
            }
        }
    }

    private void start(ICallback callback) {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        if (callback != null) {
                            callback.onGetPhoto(pathList);
                        }
                    }
                });
    }

    private interface ICallback {

        void onGetPhoto(ArrayList<String> paths);

    }
}
